package com.example.gustavo.hello.view;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Parcelable;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 *
 * Created by gustavo on 2015/3/18.
 */
public class DragLayout extends LinearLayout {

    private static final String TAG = "DragLayout";
    private View menu;
    private View main;
    private ViewDragHelper viewDragHelper;

    public DragLayout(Context context) {
        this(context,null);
    }

    public DragLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        viewDragHelper = ViewDragHelper.create(this, 1f, callback);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i(TAG,"onMeasure ="+widthMeasureSpec+" + "+heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Log.i(TAG,"onLayout changed = " + changed);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG,"onDraw");
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        menu = getChildAt(0);
        main = getChildAt(1);

    }

    private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
        @Override
        public boolean tryCaptureView(View child, int pointerId) {

            return true;
        }



        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {


            return left;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {

            return top;
        }
    };


    @Override
    public boolean onInterceptHoverEvent(MotionEvent event) {

        return viewDragHelper.shouldInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        try {
            viewDragHelper.processTouchEvent(event);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
